package jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

//冠梁查询
public class JDBCDemo11 {
    public static void main(String[] args) {
        //查询 王克晶 所带班级的信息
//        try (Connection connection = DBUtil.getConnection()){
//            String sql = "SELECT t.name,t.gender,c.name,c.floor   FROM teacher t LEFT JOIN class c ON t.id=c.teacher_id WHERE t.name=?";
//            PreparedStatement ps = connection.prepareStatement(sql);
//            ps.setString(1,"王克晶");
//            ResultSet rs = ps.executeQuery();
//            if(rs.next()){
////                String tname = rs.getString("t.name");
////                String tgender = rs.getString("t.gender");
////                String cname = rs.getString("c.name");
////                int floor = rs.getInt("c.floor");
//                //另外一种方式，但是这种方式写的话，可读性不是很好，建议按照上面那种方式写
//                String tname = rs.getString(1);
//                String tgender = rs.getString(2);
//                String cname = rs.getString(3);
//                int floor = rs.getInt(4);
//                System.out.println(tname+","+tgender+","+cname+","+floor);
//            }
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
        //查询 王克晶 所带班级的信息
        try (Connection connection = DBUtil.getConnection()){
            String sql = "SELECT t.name,t.gender,c.name,c.floor FROM teacher t  LEFT JOIN class c ON t.id=c.teacher_id WHERE t.name=?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,"王克晶");
            ResultSet rs = ps.executeQuery();
            if (rs.next()){
                String tname = rs.getString("t.name");
                String tgender = rs.getString("t.gender");
                String cname = rs.getString("c.name");
                int cfloor = rs.getInt("c.floor");
                System.out.println(tname+","+tgender+","+cname+","+cfloor);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
